package com.wonderful.eyepetizer.ui.pages.discover

import androidx.compose.foundation.ExperimentalFoundationApi
import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.width
import androidx.compose.foundation.pager.HorizontalPager
import androidx.compose.foundation.pager.PagerState
import androidx.compose.foundation.pager.rememberPagerState
import androidx.compose.material.Tab
import androidx.compose.material.Text
import androidx.compose.material3.Divider
import androidx.compose.material3.LocalContentColor
import androidx.compose.material3.TabRow
import androidx.compose.material3.TabRowDefaults.tabIndicatorOffset
import androidx.compose.runtime.Composable
import androidx.compose.runtime.LaunchedEffect
import androidx.compose.runtime.rememberCoroutineScope
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.res.stringResource
import androidx.compose.ui.unit.dp
import androidx.compose.ui.unit.sp
import androidx.compose.ui.zIndex
import androidx.lifecycle.viewmodel.compose.viewModel
import com.wonderful.eyepetizer.R
import com.wonderful.eyepetizer.ui.theme.NormalItemColor
import com.wonderful.eyepetizer.view.TitleBarWidget
import kotlinx.coroutines.launch

/**
 * Desc: 发现
 * @Author: wonderful
 * @Time: 2024/1/19 16:47
 */

val tabs = listOf("关注", "分类", "专题", "资讯", "推荐")

@OptIn(ExperimentalFoundationApi::class)
@Composable
fun DiscoverPage(viewModel: DiscoverViewModel = viewModel()) {
    val pagerState = rememberPagerState(pageCount = { tabs.size })
    val coroutineScope = rememberCoroutineScope()

    LaunchedEffect(key1 = pagerState.currentPage, block = {
        viewModel.tabIndex.intValue = pagerState.currentPage
    })

    Column(modifier = Modifier.fillMaxSize()) {
        TitleBarWidget(title = stringResource(id = R.string.discover))
        DiscoverTabRowWidget(selectIndex = viewModel.tabIndex.intValue, onTabClick = { index ->
            coroutineScope.launch {
                viewModel.tabIndex.intValue = index
                pagerState.animateScrollToPage(index)
            }
        })
        DiscoverTabPageWidget(
            pagerState = pagerState,
            modifier = Modifier
                .weight(1f)
                .zIndex(-1f)
        )
    }
}

@Composable
fun DiscoverTabRowWidget(selectIndex: Int, onTabClick: (Int) -> Unit) {
    TabRow(selectedTabIndex = selectIndex, containerColor = Color.White, indicator = {
        Box(
            modifier = Modifier.tabIndicatorOffset(it[selectIndex]),
            contentAlignment = Alignment.Center
        ) {
            Divider(
                modifier = Modifier.width(60.dp),
                thickness = 3.dp,
                color = LocalContentColor.current
            )
        }
    }) {
        tabs.forEachIndexed { index, title ->
            Tab(
                selected = selectIndex == index,
                onClick = { onTabClick(index) },
                text = {
                    Text(text = title, fontSize = 14.sp)
                },
                selectedContentColor = Color.Black,
                unselectedContentColor = NormalItemColor
            )
        }
    }
}

@OptIn(ExperimentalFoundationApi::class)
@Composable
fun DiscoverTabPageWidget(pagerState: PagerState, modifier: Modifier = Modifier) {
    HorizontalPager(
        modifier = modifier,
        state = pagerState
    ) { pageIndex ->
        when (pageIndex) {
            0 -> FollowPage()
            1 -> CategoryPage()
            2 -> TopicPage()
            3 -> NewsPage()
            4 -> RecommendPage()
        }
    }
}